<?php

Prado::using("Application.common.string");
class ProductRecord extends TActiveRecord
{
	const TABLE='tbl_product';

	public $ID;
	public $MfID;
	public $BrandID;
	public $DiscountID;
	public $Alias;
	public $SKU;
	public $Name;
	public $Price;
	public $Stock;
	public $FreeText;
	public $Brief;
	public $Description;
	public $ImagePath;
	public $ThumbnailPath;
	public $RewardPoints;
	public $IsBestSeller;
	public $IsHotDeal;
	public $IsNewArrival;
	public $IsPublished;
	public $Ordering;
	public $AvailableDate;
	public $ImageNewArrivalPath;
	public $ImageHotDealPath;
	public $ImageBestSellerPath;
	public $CreateBy;
	public $CreateDate;
	public $ModifyBy;
	public $ModifyDate;	
	
	public static $COLUMN_MAPPING=array
	(
		'product_id'=>'ID',
		'mf_id'=>'MfID',
		'brand_id'=>'BrandID',
		'discount_id'=>'DiscountID',
		'product_sku'=>'SKU',
		'product_alias'=>'Alias',
		'product_name'=>'Name',
		'product_price'=>'Price',
		'product_stock'=>'Stock',
		'product_free_text'=>'FreeText',
		'product_brief'=>'Brief',
		'product_desc'=>'Description',
		'product_image'=>'ImagePath',
		'product_thumb'=>'ThumbnailPath',
		'product_reward_points'=>'RewardPoints',
		'product_best_seller'=>'IsBestSeller',
		'product_hot_deal'=>'IsHotDeal',
		'product_new_arrival'=>'IsNewArrival',
		'product_publish'=>'IsPublished',
		'product_order'=>'Ordering',
		'product_available_date'=>'AvailableDate',
		'image_new_arrival'=>'ImageNewArrivalPath',
		'image_hot_deal'=>'ImageHotDealPath',
		'image_best_seller'=>'ImageBestSellerPath',
		'c_by'=>'CreateBy',
		'c_date'=>'CreateDate',
		'm_by'=>'ModifyBy',
		'm_date'=>'ModifyDate'		
	);
	
	public static $RELATIONS=array
	(
		'Categories'=>array(self::MANY_TO_MANY,'CategoryRecord','tbl_product_cat')
	);

	public static function finder($className=__CLASS__)
	{
		return parent::finder($className);
	}
	
	public function getCategoryIDs()
	{
		$criteria = new TActiveRecordCriteria;
		$criteria->Condition = "product_id = " . $this->ID;
		$categories = ProductCatRecord::finder()->findAll($criteria);				
		
		if (count($categories>0))
		{
			$id = array();
			foreach ($categories as $cat) 
			{
				$id[] = $cat->CatID;																															
			}
			return $id;
		}
		return null;
	}
	
	public function getCategoryNames()
	{	
		$criteria = new TActiveRecordCriteria;
		$criteria->Condition = "product_id = " . $this->ID;
		$categories = ProductCatRecord::finder()->findAll($criteria);	
		
		if (count($categories>0))
		{
			$name = "";
			for($i=0;$i<count($categories);$i++)
			{
				$name .= ($i==0?"":", ");
				
				$category = CategoryRecord::finder()->findByPk($categories[$i]->CatID);				
				$name .= $category->Name;
				
				if ($category->ParentID > 0)
				{
					$category = CategoryRecord::finder()->findByPk($category->ParentID);				
					$name .= " (".$category->Name.")";
				}
			}
			return $name;
		}		
		return "--";
	}
	
	public function save()
	{
		if ($this->ID <= 0)
		{
			$criteria = new TActiveRecordCriteria;
			$criteria->Condition = "product_id <> 0";
			$this->Ordering = self::finder()->count($criteria)+1;
			$this->CreateDate = time();
			$this->SKU = $this->SKU.rand(10000,99999); // generate auto SKU
		}
		$this->Alias = String::removeAccents(strlen($this->Alias) > 0 ? $this->Alias : $this->Name);
		$this->ModifyDate = time();
		if (strlen($this->Stock) == 0) $this->Stock = 0;
		$this->RewardPoints = 0;
		$this->AvailableDate = 0;
		parent::save();
	}
	
	public function getAllItems($publishedOnly=false)
	{
		$criteria = new TActiveRecordCriteria;
		$criteria->Condition = "product_id > 0";
		if ($publishedOnly) $criteria->Condition .= " and product_publish = 1";
		return self::finder()->findAll($criteria);
	}
	
	public function getDiscountPrice($originalPrice=0)
	{
		$discount = DiscountRecord::finder()->findByPk($this->DiscountID);
		if ($discount instanceof DiscountRecord && $discount->StartDate <= time() && $discount->EndDate >= time())
		{
			if ($discount->IsPercent)
				$originalPrice = $originalPrice-($originalPrice*$discount->Amount);
			else $originalPrice = $originalPrice-$discount->Amount;
		}
		return $originalPrice;
	}
	
	public function getBrandName() {
		return $this->Brand?$this->Brand->Name:''; 
	}
}
?>